﻿using MESPro.DBManager;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using MESPro.Models;
using Mapster;
using System.Xml.Linq;
using SqlSugar;

namespace MESPro.Views
{
    public partial class AddFunction : BaseAddFrm
    {
        #region Public Constructors

        public AddFunction()
        {
            InitializeComponent();
        }

        public AddFunction(FunctionGroup fg)
        {
            InitializeComponent();
            this.fg = fg;
            this.Load += AddFunctionGroup_Load;

        }

        #endregion Public Constructors

        #region 字段

        public List<Function> src = new();
        public List<Function> dic = new();
        /// <summary>
        /// 传进来的function组
        /// </summary>
        public FunctionGroup? fg;

        #endregion 字段

        #region Private 方法

        private void AddFunctionGroup_Load(object sender, EventArgs e)
        {
            if (fg == null) return;
            var noadd = DbContext.Db.Queryable<Function>()
                             .Where(x => !fg.Functions.Any(y => y.Id == x.Id))
                             .ToList();

            src = noadd.Adapt<List<Function>>();

            try
            {
                //List<Function> aa = fg.Functions.ToList();
                //var ssss = aa.Adapt<List<Function>>();
                dic = fg.Functions;

                this.listBox_src.DataSource = src;
                this.listBox_dic.DataSource = dic;

                listBox_src.DisplayMember = nameof(FunctionGroup.DisName);
                listBox_src.ValueMember = "Id";

                listBox_dic.DisplayMember = nameof(FunctionGroup.DisName);
                listBox_dic.ValueMember = "Id";
            }
            catch (Exception ex)
            {

                throw;
            }

        }

        private void 确定(object sender, EventArgs e)
        {
            //这里是多对多的关系
            fg.Functions = dic;
            List<FunctionGroup> List = new List<FunctionGroup>() { fg };
            var res = DbContext.Db.UpdateNav(List)
                         .Include(z1 => z1.Functions)
                         .ExecuteCommand();
            MainServerFrm.RMShowMsg?.Invoke(res ? "ok" : "ng", Enums.MsgLevel.Trace);
            this.Close();
        }

        private void 左边移动到右边(object sender, EventArgs e)
        {
            var s = this.listBox_src.SelectedItems.Cast<Function>().ToList();//要添加的
            var d = this.listBox_dic.SelectedItems.Cast<Function>().ToList();//要删除的
            foreach (var x in s)
            {
                src.Remove(x);
                dic.Add(x);
            }
            this.listBox_src.DataSource = null;
            this.listBox_dic.DataSource = null;
            this.listBox_src.DataSource = src;
            this.listBox_dic.DataSource = dic;
            listBox_src.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_src.ValueMember = "Id";

            listBox_dic.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_dic.ValueMember = "Id";
        }

        private void 右边移动到左边(object sender, EventArgs e)
        {
            var s = this.listBox_src.SelectedItems.Cast<Function>().ToList();//要添加的
            var d = this.listBox_dic.SelectedItems.Cast<Function>().ToList();//要删除的
            foreach (var x in d)
            {
                dic.Remove(x);
                src.Add(x);
            }
            this.listBox_src.DataSource = null;
            this.listBox_dic.DataSource = null;
            this.listBox_src.DataSource = src;
            this.listBox_dic.DataSource = dic;
            listBox_src.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_src.ValueMember = "Id";

            listBox_dic.DisplayMember = nameof(FunctionGroup.DisName);
            listBox_dic.ValueMember = "Id";
        }

        #endregion Private 方法
    }
}
